<!DOCTYPE html>
<html lang="zh-CN">
<meta>
    <meta charset="utf-8"></meta>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<table class="table">
    <caption>Optional table caption.</caption>
    <thead>
    <tr>
        <th>#</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Username</th>
        <th>Username1</th>
    </tr>
    </thead>
    <tbody>
    <form id="form">
        <div class="checkbox">
            <label>
                <input name="mark" value="1" type="checkbox" id="retainQuestion" checked> Check me out
            </label>
        </div>
        <tr>
            <th scope="row">1</th>
            <td><input type="text" name="one" value=112.12></td>
            <td><input type="text" name="two" value=122.23></td>
            <td><input type="text" name="three" value=233.23></td>
            <td><input type="text" name="four" value=3243.12></td>
        </tr>
        <tr>
            <th scope="row">2</th>
            <td><input type="text" name="one" value=152.12></td>
            <td><input type="text" name="two" value=126.23></td>
            <td><input type="text" name="three" value=273.23></td>
            <td><input type="text" name="four" value=3283.12></td>
        </tr>
        <tr>
            <th scope="row">3</th>
            <td><input type="text" name="one" value=1322.12></td>
            <td><input type="text" name="two" value=1232.23></td>
            <td><input type="text" name="three" value=22343.23></td>
            <td><input type="text" name="four" value=32553.12></td>
        </tr>
        <button type="button" onclick="listupata()" class="btn btn-default">Submit</button>
    </form>
    </tbody>
</table>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
<script type="application/javascript">
    //发送表单ajax请求
    function listupata() {
        var formObject = [];
        var formArray = $("#form").serializeArray();
        $.each(formArray, function (i, item) {
            var tmp = {}
            tmp[item.name] = item.value;
            formObject.push(tmp)
        });

        var data = JSON.stringify(formObject);
        console.log(data);


        // $.ajax({
        //     url: "/list",
        //     type: "POST",
        //     data: JSON.stringify(formObject),
        //     contentType: "application/json;charset=UTF-8",  //缺失会出现URL编码，无法转成json对象
        //     success: function (data) {
        //         alert(data);
        //     },
        //     error: function (e) {
        //         alert("错误！！");
        //     }
        // });
    }


    /**
     * 自动将form表单封装成json对象
     */
    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
</script>